package com.foreveross.weixin.service;

import java.util.Map;

import javax.annotation.Resource;

import com.foreveross.service.OrderInfoService;
import com.foreveross.util.XmlUtil;
import com.foreveross.vo.OrderInfo;
import com.foreveross.weixin.common.Constant;
import com.foreveross.weixin.util.ObjectToMap;
import com.foreveross.weixin.util.SignUtil;
import com.foreveross.weixin.util.StringUtil;
import com.foreveross.weixin.util.WeixinUtil;
import com.foreveross.weixin.vo.pay.PayOrderQueryResult;
import com.foreveross.weixin.vo.pay.PayRequestVo;
import com.foreveross.weixin.vo.pay.PayUnifiedOrder;
import com.foreveross.weixin.vo.pay.PayUnifiedOrderResult;

public class PayUnifiedOrderService {
	
	/**
	 * H5 支付 统一下单
	 * @param body
	 * @param total_fee
	 * @param Spbill_create_ip
	 * @param openid
	 * @param notify_url
	 * @param appId
	 * @param mchId
	 * @param apiKey
	 * @return
	 * @throws Exception
	 */
	public static PayRequestVo sendPay(String body,String total_fee,String Spbill_create_ip,
			String openid,String notify_url,String appId,String mchId,String apiKey) throws Exception{
		String nonceStr = StringUtil.create_nonce_str();
		PayUnifiedOrder payUnifiedOrder = new PayUnifiedOrder();
		payUnifiedOrder.setAppid(appId);
		payUnifiedOrder.setMch_id(mchId);
		payUnifiedOrder.setNonce_str(nonceStr);
		payUnifiedOrder.setBody(body);
		payUnifiedOrder.setOut_trade_no(StringUtil.getTradeNo());
		payUnifiedOrder.setTotal_fee(total_fee);
		payUnifiedOrder.setSpbill_create_ip(Spbill_create_ip);
		payUnifiedOrder.setOpenid(openid);
		payUnifiedOrder.setTrade_type(Constant.TRADE_TYPE);
		payUnifiedOrder.setNotify_url(notify_url);
		
		String sign = SignUtil.getPayCustomSign(ObjectToMap.objectToMapViaFields(payUnifiedOrder, false), apiKey);
		payUnifiedOrder.setSign(sign);
		String xmlStr = XmlUtil.ClazzToXml(payUnifiedOrder);
		System.out.println(xmlStr);

		PayUnifiedOrderResult payUnifiedOrderResult = WeixinUtil.getPayUnifiedOrderResult(xmlStr);
		String prepayId = null;
		if(payUnifiedOrderResult.getReturn_code().equals("SUCCESS") ){
			prepayId = payUnifiedOrderResult.getPrepay_id();
		}
		
		PayRequestVo payRequestVo = new PayRequestVo();
		payRequestVo.setAppId(appId);
		payRequestVo.setNonceStr(nonceStr);
		payRequestVo.setTimeStamp(StringUtil.create_timestamp());
		payRequestVo.setSignType("MD5");
		Map map = ObjectToMap.objectToMapViaFields(payRequestVo, false);
		map.put("package", "prepay_id="+prepayId);
		String paySign = SignUtil.getPayCustomSign(map, apiKey);
		payRequestVo.setPaySign(paySign);
		payRequestVo.setPrepay_id(prepayId);
		payRequestVo.setOutTradeNo(payUnifiedOrder.getOut_trade_no());
		return payRequestVo;
	}
	

	public static PayOrderQueryResult getPayOrderQueryResult(String nonceStr,
			String appId, String mchId,String apiKey, String outTradeNo) throws Exception{
		PayUnifiedOrder payUnifiedOrder = new PayUnifiedOrder();
		payUnifiedOrder.setAppid(appId);
		payUnifiedOrder.setMch_id(mchId);
		payUnifiedOrder.setNonce_str(nonceStr);
		payUnifiedOrder.setOut_trade_no(outTradeNo);
		String sign = SignUtil.getPayCustomSign(ObjectToMap.objectToMapViaFields(payUnifiedOrder, false), apiKey);
		payUnifiedOrder.setSign(sign);
		String xmlStr = XmlUtil.ClazzToXml(payUnifiedOrder);
		System.out.println(xmlStr);

		PayOrderQueryResult payOrderQueryResult = WeixinUtil.getPayOrderQueryResult(xmlStr);
		return payOrderQueryResult;
	}
	
	
	public static void main(String[] args) {
		//PayUnifiedOrderService PayUnifiedOrderService = new PayUnifiedOrderService();
		try {
			StringBuffer str = new StringBuffer();

	/*		getPayOrderQueryResult("5855043d-f0c1-44ad-b91b-da751581", "wx30127f21c9a69197", 
					"1265924701", "HRNO20151013045751");*/
			//PayOrderQueryResult payUnifiedOrderResult = WeixinUtil.getPayOrderQueryResult(str.toString());
			  
			
			//PayRequestVo payRequestVo =PayUnifiedOrderService.sendPay("123", "123", "127.0.0.1", "oEJ6GwLd3fldVKk3QspfNIjw9svU", "www.baidu.com");
			//System.out.println(payRequestVo);
			
			
			
/*			PayUnifiedOrder payUnifiedOrder = new PayUnifiedOrder();
			payUnifiedOrder.setAppid(Constant.appId);
			payUnifiedOrder.setMch_id(Constant.MCH_ID);
			payUnifiedOrder.setNonce_str("2554eea5-1607-4eb2-9de1-e04cdbd5");
			payUnifiedOrder.setBody("实惠");
			payUnifiedOrder.setOut_trade_no("HRNO20151008021329");
			payUnifiedOrder.setTotal_fee("120");
			payUnifiedOrder.setSpbill_create_ip("125.86.117.10");
			payUnifiedOrder.setOpenid("oEJ6GwJna4O1kgDpSTafR26KGolY");
			payUnifiedOrder.setTrade_type(Constant.TRADE_TYPE);
			payUnifiedOrder.setNotify_url("www.baidu.com");
			
			String sign = SignUtil.getPayCustomSign(ObjectToMap.objectToMapViaFields(payUnifiedOrder, false), Constant.payAPIKey);
			System.out.println(sign);*/
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
